package com.aiit.config;

import org.springframework.context.annotation.Configuration;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

@Configuration
public class BookingStatusScanner {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/teahouse";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "123456";

    public static void main(String[] args) {
        updateBookingStatus();
    }

    public static void updateBookingStatus() {
        // 计算当前时间减去 5 分钟
        LocalDateTime fiveMinutesAgo = LocalDateTime.now().minusMinutes(5);
        long timestamp = fiveMinutesAgo.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();

        String sql = "UPDATE bookings SET status = 3 WHERE session_time < ?";

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setLong(1, timestamp);

            int rowsAffected = pstmt.executeUpdate();
            System.out.println("Updated " + rowsAffected + " rows.");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}    